package com.syg.orm;

import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 为了代替数据库
 *
 * @author oe_shenyiguang
 */
@Component
public class SpringScheduledCronRepository {

    List<SpringScheduledCron> list = new ArrayList<>();

    Map<String, SpringScheduledCron> map = new HashMap<>();

    /**
     * 构造数据库返回的数据
     */
    @PostConstruct
    public void init() {
        SpringScheduledCron cron1 = new SpringScheduledCron();
        cron1.setCronId("task01");
        cron1.setCronKey("com.syg.scheduled.DynamicTask01");
        cron1.setCronExpression("*/10 * * * * ?");
        cron1.setTaskExplain("这是任务1");
        cron1.setStatus("0");

        SpringScheduledCron cron2 = new SpringScheduledCron();
        cron2.setCronId("task02");
        cron2.setCronKey("com.syg.scheduled.DynamicTask02");
        cron2.setCronExpression("*/10 * * * * ?");
        cron2.setTaskExplain("这是任务2");
        cron2.setStatus("1");

        SpringScheduledCron cron3 = new SpringScheduledCron();
        cron3.setCronId("task03");
        cron3.setCronKey("com.syg.scheduled.DynamicTask03");
        cron3.setCronExpression("*/10 * * * * ?");
        cron3.setTaskExplain("这是任务3");
        cron3.setStatus("0");

        list.add(cron1);
        list.add(cron2);
        list.add(cron3);

        map.put(cron1.getCronKey(), cron1);
        map.put(cron2.getCronKey(), cron2);
        map.put(cron3.getCronKey(), cron3);
    }

    /**
     * 查表中所有的数据
     *
     * @return
     */
    public List<SpringScheduledCron> findAll() {
        return list;
    }

    /**
     * 按照定时任务完整类名查找
     *
     * @param cronKey
     * @return
     */
    public SpringScheduledCron findByCronKey(String cronKey) {
        return map.get(cronKey);
    }

}
